package com.czmh.etc.onlinepubsys.services.impl;

import com.czmh.etc.onlinepubsys.feign.newrun.EtcApplyInfoClient;
import com.czmh.etc.onlinepubsys.feign.newrun.constans.UserProperty;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.czmh.etc.onlinepubsys.maper.EtcApplyMapper;
import com.czmh.etc.onlinepubsys.model.EtcApply;
import com.czmh.etc.onlinepubsys.services.EtcApplyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName：YueServiceImpl
 * @Description：TODO 
 * @Author： sevenchen. 
 * @Date：2019/10/18 11:50. 
 * @Version：1.0.0
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class EtcApplyInfoServiceImpl implements EtcApplyInfoService {
    @Autowired
    private EtcApplyInfoClient etcApplyInfoClient;
    @Autowired
    private EtcApplyMapper etcApplyMapper;

    @Autowired
    private UserProperty userProperty;
    
    @Override
    public JsonResult selectApplyRecord() {
        log.info("========获取申请记录列表=======");
        JsonResult jsonResult = etcApplyInfoClient.selectApplyRecord();
        // 获取纽润返回的申请记录结果返回，同时更新本地的申请状态信息
        if (JsonResult.CODE_SUCCESS.equals(jsonResult.getCode().toString())) {
            List<Object> data = (List<Object>) jsonResult.getData();
            List<EtcApply> list = new ArrayList<EtcApply>();
            if (data != null && !data.isEmpty()) {
                EtcApply update = null;
                for (int i = 0, size = data.size(); i < size; i++) {
                    Map<String, Object> map = (Map<String, Object>) data.get(i);
                    update = new EtcApply();
                    update.setNiurunApplyId(map.get("applyId").toString());
                    update.setApply_status(Integer.valueOf(map.get("status").toString()));
                    list.add(update);
                }
                etcApplyMapper.batchUpdateApplyStatus(list);
                log.info("========更新申请记录状态=======");
            }
            
        }
        return jsonResult;
    }

}
